package hibernate.dao;

import hibernate.entity.Cliente;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import java.util.List;

public class ClienteDAO {
	
	private Session session;
	
	public ClienteDAO(){
		
	}
	
	public void adiciona(Cliente cliente){		
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();		
		Transaction transaction = session.beginTransaction();
				
    	try{
			session.save(cliente);
			transaction.commit();
		} catch(Exception e){
			if(transaction != null)
				transaction.rollback();
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}			
	}	

	public void altera(Cliente cliente){		
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();		
		Transaction transaction = session.beginTransaction();
				
    	try{
			session.update(cliente);
			transaction.commit();
		} catch(Exception e){
			if(transaction != null)
				transaction.rollback();
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}
	}	

	public void remove(Cliente cliente){
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();		
		Transaction transaction = session.beginTransaction();
				
    	try{
			session.delete(cliente);
			transaction.commit();
		} catch(Exception e){
			if(transaction != null)
				transaction.rollback();
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}	
	}	
	
	public Cliente importaDados(String nome){
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();			
		Criteria criteria = session.createCriteria(Cliente.class);
		Cliente cliente = null;
		
    	try{
    		criteria.add(Restrictions.eq("nome", nome));
    		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    		cliente = (Cliente) criteria.uniqueResult();
		} catch(Exception e){
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}			
		
		return cliente;
	}
	
	public List<Cliente> getLista(){
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();			
		Criteria criteria = session.createCriteria(Cliente.class);		
		List<Cliente> listaClientes = null;
		
    	try{
			listaClientes = (List<Cliente>) criteria.list();
		} catch(Exception e){
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}			
		return listaClientes;
	}
}